package com.example.action;

import com.example.bean.Product;
import com.example.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 前十二个热门商品
 */
@WebServlet("/index")
public class HotProductServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 判断是否登录
        if (req.getSession().getAttribute("loginUser") != null) {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            ProductService productService = new ProductService();
            List<Product> hotProducts = productService.selectHotProduct();
            req.setAttribute("list", hotProducts);
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("login.jsp");
        }
    }
}